/**
 *
 * @description vlidate 验证 中间件
 *
 * **/

const { jsonSchemaFileInfo }  = require('../model/ErrorInfoModel')
const { ErrorResModel } = require('../model/ResModel')

/**
 * @param {func} validateFn
 * **/
const genValidator = (validateFn) => {
  async function validator (ctx, next) {
    const error = validateFn(ctx.request.body)
    if(error) {
      ctx.body = new ErrorResModel(jsonSchemaFileInfo)
    } else {
      await next()
    }

  }
  return validator
}

module.exports = {
  genValidator: genValidator,
}